package lib

//link to 227 282 
func init() {
	Probs = append(Probs, Problem{
		Num:         224,
		Discription: "只有+-()的计算器",
		Level:       3,
		Labels: map[string]int{
			"栈": 1,
			"符号栈": 1,
			"虚拟栈": 1,
		},
	})
}

//思想：消括号，把括号的影响集中在运算符，然后做正常的加减
func Calculate(s string) int {
	res := 0
	//将每个括号前的运算符（抵消后的）进栈
	ops := []int{1}
	//当前的运算符（抵消后的）
	curOps := 1
	i := 0
	for i < len(s) {
		switch s[i] {
		case '+':
			curOps = ops[len(ops)-1]
			i++
		case '-':
			curOps = -ops[len(ops)-1]
			i++
		case '(':
			ops = append(ops, curOps)
			i++
		case ')':
			ops = ops[:len(ops)-1]
			i++
		case ' ':
			//空格不管
			i++
		default:
			num := 0
			for i < len(s) && s[i] >= '0' && s[i] <= '9' {
				num = num*10 + int(s[i]-'0')
				i++
			}
			res += curOps * num
		}
	}

	return res
}
